﻿using MyProject.Entities.MyDialogResult;
using MyProject.Entities.User;
using MyProject.Services.ListItem;
using MyProject.ViewsModel.UserDialog;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ListItem = MyProject.Entities.ListItem.ListItem;

namespace MyProject.Views.UserDialog
{
    /// <summary>
    /// Логика взаимодействия для UserDialog.xaml
    /// </summary>
    public partial class UserDialog : Window
    {
        public new MyDialogResult<User>? DialogResult { get; set; }
        private UserDialogContext _context;
        public UserDialog(User userData)
        {
            if (userData.id == 0 || userData.id == null)
            {
                this.Title = "Добавление сотрудника";
            }
            else
            {
                this.Title = "Редактирование сотрудника";
            }

            this._context = new UserDialogContext(this, userData);
            this.DataContext = this._context;

            InitializeComponent();

            if (userData.id != null)
            {
                if (userData.registration != null && userData.registration.city_id != null)
                {
                    ComboBox streetControl = (ComboBox)this.FindName("Address_street_id");
                    if (streetControl != null)
                    {
                        streetControl.IsReadOnly = false;
                        streetControl.ToolTip = null;

                        List<object> list  = new List<object>();
                        list.Add(new ListItem("", 0, "", userData.registration.city_id));
                        this._context.RegistrationCitySelectionChanged(streetControl, list);
                        streetControl.SelectedValue = userData.registration.street_id;
                    }
                }

                if (userData.place_residence != null && userData.place_residence.city_id != null)
                {
                    ComboBox streetControl = (ComboBox)this.FindName("Address_1_street_id");

                    if (streetControl != null)
                    {
                        streetControl.IsReadOnly = false;
                        streetControl.ToolTip = null;
                        List<object> list = new List<object>();
                        list.Add(new ListItem("", 0, "", userData.registration.city_id));
                        this._context.RegistrationCitySelectionChanged(streetControl, list);
                    }
                }

            }
        }

        private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void DateValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            DatePicker contorl = (DatePicker)sender;
            int textLength = contorl.Text.Count();
            if (textLength >= 10 || (e.Text == "." && (textLength != 2 || textLength != 7)))
            {
                e.Handled = true;
                return;
            }

            Regex regex = new Regex("[^0-9.]+");
            e.Handled = regex.IsMatch(e.Text);
        }

        private void RegistrationCitySelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox streetControl = (ComboBox)this.FindName("Address_street_id");
            if (streetControl != null)
            {
                streetControl.IsReadOnly = false;
                streetControl.ToolTip = null;
                this._context.RegistrationCitySelectionChanged(streetControl, e.AddedItems, true);
            }
        }

        private void ResidenceCitySelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox streetControl = (ComboBox)this.FindName("Address_1_street_id");
            if (streetControl != null)
            {
                streetControl.IsReadOnly = false;
                streetControl.ToolTip = null;
                this._context.ResidenceCitySelectionChanged(streetControl, e.AddedItems, true);
            }
        }
    }
}
